var ipconfig = "http://192.168.3.133:8080"//服务器ip地址
Page({
  data: {
    showTopTips: false,
    user:'',
    coords:'',
    yearstart:'2018-01-01',
    yearend:'2050-01-01',
    index: 0,
    radioItems: [
      { name: 'cell standard', value: '0' },
      { name: 'cell standard', value: '1', checked: true }
    ],
    checkboxItems: [
      { name: 'standard is dealt for u.', value: '0', checked: true },
      { name: 'standard is dealicient for u.', value: '1' }
    ],

    date: "2018-01-01",
    time:'00:00',
    index:0,


    sex: ["男", "女"],
    sexIndex: 0,



    isAgree: false
  },
  onLoad:function(){
    var that=this;
    wx.request({
      url: ipconfig+"/WxAdds",
      method: "post",
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      success:function(res){
      console.log(res.data)
      that.setData({
          user:res.data[0],
          coords:"桐梓林一号"+res.data[2]+"栋"+res.data[1]+"单元"+res.data[3]+"号"
      })
      }
    })
  },


  showTopTips: function () {

   

  },
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value);

    var radioItems = this.data.radioItems;
    for (var i = 0, len = radioItems.length; i < len; ++i) {
      radioItems[i].checked = radioItems[i].value == e.detail.value;
    }

    this.setData({
      radioItems: radioItems
    });
  },
  checkboxChange: function (e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value);

    var checkboxItems = this.data.checkboxItems, values = e.detail.value;
    for (var i = 0, lenI = checkboxItems.length; i < lenI; ++i) {
      checkboxItems[i].checked = false;

      for (var j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (checkboxItems[i].value == values[j]) {
          checkboxItems[i].checked = true;
          break;
        }
      }
    }

    this.setData({
      checkboxItems: checkboxItems
    });
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  },
  formSubmit:function(e){
  var that=this;
  console.log(that.data.time)
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    var userName = e.detail.value.userName;
    var peoples = e.detail.value.peoples;
    var time=that.data.time;
    var year=that.data.date;
    var user=that.data.user;
    var srt = /^(?!0)\d{1,20}$/;
    var username = /^[\u4E00-\u9FA5]+$/;
    if (userName == "" || peoples == "") {
      wx.showToast({
        title: '请完善信息',
        image: '../imgages/failure.png',
      })
    }else{
    if (username.test(userName) && srt.test(peoples)){
      wx.request({
        url: ipconfig+"/WxAddVisitor",
        method: "post",
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Cookie': wx.getStorageSync("session")
        },
        success:function(res){
          console.log(res.data)
          if(res.data==true){
            wx.redirectTo({
              url: '../subpage/visitor',
            })
          }
        },
        data: {
          visitor_name: userName,
          visitor_number: peoples,
          user: user,
          time: time,
          year: year,
        }
      })
    } else if (!(username.test(userName))) {
      wx.showToast({
        title: '访客姓名有误',
        image: '../imgages/failure.png',
      })
    } else if (!(srt.test(peoples))) {
      wx.showToast({
        title: '访客人数有误',
        image: '../imgages/failure.png',
      })
    }
    }
  },

  bindCountryCodeChange: function (e) {
    console.log('picker country code 发生选择改变，携带值为', e.detail.value);

    this.setData({
      countryCodeIndex: e.detail.value
    })
  },
  bindCountryChange: function (e) {
    console.log('picker country 发生选择改变，携带值为', e.detail.value);

    this.setData({
      sexIndex: e.detail.value
    })
  },
  bindAccountChange: function (e) {
    console.log('picker account 发生选择改变，携带值为', e.detail.value);

    this.setData({
      accountIndex: e.detail.value
    })
  },
  bindAgreeChange: function (e) {
    this.setData({
      isAgree: !!e.detail.value.length
    });
  },
  bindTimeChanges: function (e) {
    console.log("时间："+e.detail.value)
    this.setData({
      time: e.detail.value
    })
  },
  //  点击日期组件确定事件  
  bindDateChanges: function (e) {
    console.log("日期：" +e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
});